import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_admin/api/odoo/purchase_evaluation_project_odoo_api.dart';
import 'package:flutter_admin/models/odoo/purchase_quality_problem.dart';
import 'package:flutter_admin/models/odoo/template_tester_mapper.dart';
import 'package:flutter_admin/pages/common/cry/cry_button_bar.dart';
import 'package:flutter_admin/pages/common/cry/cry_buttons.dart';
import 'package:flutter_admin/pages/common/cry/cry_data_table.dart';
import 'package:flutter_admin/pages/common/cry/model/order_item_model.dart';
import 'package:flutter_admin/pages/common/cry/model/page_model.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/odoo/purchase_supplier_evaluation/add_template_tester_map_dialog.dart';
import 'package:flutter_admin/utils/adaptive_util.dart';
import 'package:get/get.dart';

class SupplierEvaluationTemplateConfig extends StatefulWidget {
  SupplierEvaluationTemplateConfig({Key? key}) : super(key: key);

  @override
  State<SupplierEvaluationTemplateConfig> createState() =>
      _SupplierEvaluationTemplateConfigState();
}

class _SupplierEvaluationTemplateConfigState
    extends State<SupplierEvaluationTemplateConfig> {
  final GlobalKey<CryDataTableState> tableKey = GlobalKey<CryDataTableState>();
  PageModel page =
      PageModel(orders: [OrderItemModel(column: 'create_date', asc: false)]);
  List selectedList = [];
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    WidgetsBinding.instance!.addPostFrameCallback((c) {
      tableKey.currentState?.pageTo(0);
      _loadData();
    });
  }

  @override
  Widget build(BuildContext context) {
    CryDataTable table = CryDataTable(
      key: tableKey,
      title: "数据列表",
      actions: [],
      onPageChanged: (firstRowIndex) {
        page.current = (firstRowIndex ~/ page.size + 1);
        _loadData();
      },
      onRowsPerPageChanged: (int size) {
        page.size = size;
        page.current = 1;
        _loadData();
      },
      onSelectChanged: (Map selected) {
        this.setState(() {});
      },
      columns: getTableColumns(),
      getCells: (m) {
        var mapper = TemplateTesterMapper.fromJson(m);
        return [
          DataCell(Text(mapper.supplier ?? '')),
          DataCell(Text(mapper.deptName ?? '')),
          DataCell(Text(mapper.testerName ?? '')),
          DataCell(Text(mapper.createDate ?? '')),
        ];
      },
    );
    CryButtonBar buttonBar = CryButtonBar(children: getTopButton());
    _getSelectList();
    return Scaffold(
      body: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          SizedBox(height: 10),
          isDisplayDesktop(context) ? buttonBar : Container(),
          Expanded(child: SingleChildScrollView(child: table)),
        ],
      ),
    );
  }

  ///
  ///
  ///@Author: chendeji
  ///@Date: 2022-05-25 14:39:09
  ///
  _loadData() async {
    ResponseBodyApi responseBodyApi =
        await PurchaseEvaluationProjectOdooApi.getTemplateTesterItems(data: {
      "page": page,
    });
    if (responseBodyApi.success!) {
      page = responseBodyApi.data != null ? responseBodyApi.data : PageModel();
      tableKey.currentState!.loadData(page);
      // _getSelectList();
    }
  }

  ///
  /// 顶部的按钮
  ///@Author: chendeji
  ///@Date: 2022-05-25 14:31:21
  ///
  getTopButton() {
    List<Widget> buttonBarList = [
      CryButtons.add(context, () {
        _add();
      }),
      CryButtons.edit(context, () {}),
      CryButtons.delete(context, () {}),
    ];
    return buttonBarList;
  }

  getTableColumns() {
    return [
      //isDisplayDesktop(context)?DataColumn(
      //  label: Text(S.of(context).operating),
      //):DataColumn(label: Container()),
      DataColumn(
        label: Text('供应商评价模板'),
        //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
      ),
      DataColumn(
        label: Text('考评部门'),
        //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
      ),
      DataColumn(
        label: Text('考评人'),
        //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
      ),
      DataColumn(
        label: Text('创建时间'),
        //onSort: (int columnIndex, bool ascending) => _sort('create_date'),
      ),
    ];
  }

  void _getSelectList() {
    selectedList = tableKey.currentState
            ?.getSelectedList(page)
            .map<TemplateTesterMapper>((e) => TemplateTesterMapper.fromJson(e))
            .toList() ??
        [];
    setState(() {});
  }

  void _add() {
    showDialog(
      context: context,
      builder: (BuildContext context) => Dialog(
        child: AddTemplateTesterMapDialog(),
      ),
    ).then((v) {
      if (v != null) {
        // _query();
      }
    });
  }
}
